package com.tanjor.sys.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tanjor.sys.entity.SysRoleMenu;
import com.tanjor.sys.dubbo.dto.SysRoleMenuDTO;
import com.tanjor.sys.mapper.SysRoleMenuMapper;
import com.tanjor.sys.service.interfaces.ISysRoleMenuService;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.tanjor.common.datatable.ColumnFilter;
import com.tanjor.common.datatable.PageReq;
import com.tanjor.common.datatable.PageResp;
import com.tanjor.common.exception.BusinessException;
import com.tanjor.common.util.ObjectUtil;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.toolkit.IdWorker;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Liuwy
 * @since 2017-04-03
 */
@Service("SysRoleMenuServiceImpl")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {
	
	/**
	 *  List<SysRoleMenu> to List<SysRoleMenuDTO>
	 */
	private  List<SysRoleMenuDTO> copyList(List<SysRoleMenu> list){
		List<SysRoleMenuDTO> entityDTOs = new ArrayList<SysRoleMenuDTO>();
		if(list!=null){
			for(SysRoleMenu entity:list){
				SysRoleMenuDTO entityDTO = new SysRoleMenuDTO();
				ObjectUtil.copy(entity,entityDTO);
				entityDTOs.add(entityDTO);
			}
		}
		return entityDTOs;
	}
	
	/**
	 * 分页入参构造器
	 * @param pageReq
	 * @return
	 */
	private EntityWrapper<SysRoleMenu> createWrapper(PageReq pageReq){
		EntityWrapper<SysRoleMenu> wrapper = new EntityWrapper<SysRoleMenu>();
		if(pageReq.getColumnFilters()!=null){
			for(ColumnFilter columnFilter:pageReq.getColumnFilters()){
				//按需求构造入参
				
			}
		}
		return wrapper;
	}
	
	@Override
	public PageResp findPage(PageReq pageReq) {
		// TODO Auto-generated method stub
		PageResp page = new PageResp();
		//请求标志位
		page.setDraw(pageReq.getDraw());
		//构造条件
		EntityWrapper<SysRoleMenu> wrapper = createWrapper(pageReq);
		//统计总数
		Integer totalRows = this.baseMapper.selectCount(wrapper);
		//排序	
		if(StringUtils.isNotBlank(pageReq.getOrderBy())){
			wrapper.orderBy(pageReq.getOrderBy(), pageReq.getIsAsc());
		}
		//查询	
		List<SysRoleMenuDTO> list = findPage(wrapper, pageReq.getStart(), pageReq.getLength());				
		
		//构造返回
		page.setData(list);
		page.setRecordsFiltered(totalRows);
		page.setRecordsTotal(totalRows);
		return page;
	}
	
	@Override
	public List<SysRoleMenuDTO> findPage(Wrapper<SysRoleMenu> wrapper,int start,int limit) {
		// TODO Auto-generated method stub
		List<SysRoleMenu> list = baseMapper.selectPage(new RowBounds(start, limit), wrapper);
		return this.copyList(list);
	}
	
	@Override
	public Map<String,Object> insert(SysRoleMenuDTO entityDTO) throws BusinessException{
		// TODO Auto-generated method stub	
		Map<String,Object> map = new HashMap<String,Object>();
		entityDTO.setId(""+IdWorker.getId());
		SysRoleMenu entity = new SysRoleMenu();
		ObjectUtil.copy(entityDTO,entity);
		this.insert(entity);
		map.put("result", true);
		return map;
	}
	
	@Override
	public Map<String,Object> update(SysRoleMenuDTO entityDTO) throws BusinessException{
		Map<String,Object> map = new HashMap<String,Object>();
		// TODO Auto-generated method stub
		SysRoleMenu entity = new SysRoleMenu();
		ObjectUtil.copy(entityDTO,entity);
		this.updateById(entity);
		map.put("result", true);
		return map;
	}	
	
	@Override
	public Map<String, Object> delById(Serializable id) throws BusinessException {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String,Object>();
		this.deleteById(id);
		map.put("result", true);	
		return map;
	}
	
	@Override
	public SysRoleMenuDTO findById(Serializable id){
		// TODO Auto-generated method stub
		SysRoleMenuDTO entityDTO = new SysRoleMenuDTO();
		SysRoleMenu entity = this.selectById(id);
		ObjectUtil.copy(entity,entityDTO);
		return entityDTO;
	}

	@Override
	public List<SysRoleMenuDTO> findByRoleCode(String roleCode) {
		// TODO Auto-generated method stub
		EntityWrapper<SysRoleMenu> wrapper = new EntityWrapper<SysRoleMenu>();
		wrapper.and("role_code={0}", roleCode);
		List<SysRoleMenu> list = this.baseMapper.selectList(wrapper);
		return this.copyList(list);
	}

	@Override
	public Map<String, Object> insert(String roleCode, String menuIds) throws BusinessException {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isEmpty(roleCode)){
			map.put("result", false);
			map.put("msg", "角色编码不能为空！");
			return map;
		}

		EntityWrapper<SysRoleMenu> wrapper = new EntityWrapper<SysRoleMenu>();
		wrapper.and("role_code={0}", roleCode);
		this.baseMapper.delete(wrapper);
		if(StringUtils.isNotBlank(menuIds)){
			String[] ids = menuIds.split(",");
			for(String menuId:ids){
				SysRoleMenu roleMenu = new SysRoleMenu();
				roleMenu.setId(""+IdWorker.getId());
				roleMenu.setMenuId(menuId);
				roleMenu.setRoleCode(roleCode);
				this.insert(roleMenu);
			}
		}
		map.put("result", true);
		
		return map;
	}
	
}
